** Constructing Binding measures

* Preparing weight

use "${rawdata}imp_detl_yearly_104.dta", clear /*10 digit US import in 2004*/
* Putting non-Mexico and non-China together
replace cty_code="9999" if cty_code~="2010" & cty_code~="5700"
rename gen_val_yr value
collapse (sum) value, by(commodity cty_code)

gen country=0 /** Mexico**/
replace country=1 if cty_code=="5700" /** China**/
replace country=2 if cty_code=="9999" /** Other**/
drop cty_code
reshape wide value, i(commodity) j(country)

rename value0 value_mex
rename value1 value_chn
rename value2 value_other
replace value_mex=0 if value_mex==.
replace value_chn=0 if value_chn==.
replace value_other=0 if value_other==.
gen value_world=value_mex+value_chn+value_other
rename commodity hs
gen hs6=substr(hs, 1, 6)
gen hs2=substr(hs, 1, 2)
destring hs2, replace
keep if hs2>=50 & hs2<=63
save "${temp_data}usimp_chinamex_2004.dta", replace

* Constructing binding measures
* Preparing the relevant part of MFA data
use "${rawdata}mfa8404.dta", clear /** MFA raw data**/
keep if  country=="CHINA" & year==104
sort mfa
keep mfa_root fr import base
gen order=_n
* Dealing with the cases where mfa_root contains two categories
gen mfa1=substr(mfa_root, 1, 3)
gen mfa2=subinstr(mfa_root, mfa1, "", .)
replace mfa2=subinstr(mfa2, "/", "", .)
reshape long mfa, i(order) j(aaa)
drop if mfa==""
drop order
collapse fr [fw=base], by(mfa)
destring mfa, replace
save "${temp_data}mfa2004china.dta", replace



use "${rawdata}hs_mfa_phase_20111208.dta", replace
merge m:1 mfa using "${temp_data}mfa2004china"
replace fr=0 if fr==.
drop _merge
tostring hs, replace
gen hs6=substr(hs, 1, 6)
gen hs2=substr(hs, 1, 2)
destring hs2, replace

** Merge with weight data
merge 1:1 hs using "${temp_data}usimp_chinamex_2004"
keep if hs2>=50 & hs2<=63
drop if mfa==. & value_mex==0 & value_chn==0

preserve
gen list=1
collapse list, by (hs6 hs2)
tempfile list
save `list'
restore

replace value_mex=0 if value_mex==.
replace value_chn=0 if value_chn==.
replace value_world=0 if value_world==.

bys hs6: egen aaa=sum(value_mex)
gen weight=value_mex/aaa

gen bind=(fr>0.8)
replace bind=0 if phase!=4
replace bind=0 if _merge==2

preserve
collapse (mean) bind80_mex=bind [w=value_mex] , by(hs2 hs6)
tempfile mex
save `mex'

restore
preserve
collapse (mean) bind80_world=bind [w=value_world] , by(hs2 hs6)
tempfile world
save `world'

restore
collapse (mean) bind80_chn=bind [w=value_chn] , by(hs2 hs6)

merge 1:1 hs6 hs2 using `mex'
drop _merge
merge 1:1 hs6 hs2 using `world'
drop _merge
merge 1:1 hs6 hs2 using `list'
drop _merge

replace bind80_mex=0 if bind80_mex==.

gen iq_mex_bind80= 1 if bind80_mex>=0.5
replace iq_mex_bind80= 0 if bind80_mex<0.5
replace iq_mex_bind80= 0 if bind80_mex==.
label var iq_mex_bind80 "1 if bind80_mex>=0.5 and 0 otherwise"

gen iq_china_bind80= 1 if bind80_chn>=0.5
replace iq_china_bind80= 0 if bind80_chn<0.5
replace iq_china_bind80= 0 if bind80_chn==.
label var iq_china_bind80 "1 if bind80_chn>=0.5 and 0 otherwise"

drop hs2

save "${output_data}quota_mex_weight_hs6.dta", replace
